package com.tanhua.server.interceptor;


import com.tanhua.model.db.User;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-08 23:25
 **/

    /*
    *操作threadlocal的工具类
    * */
public class UserHolder {

    /*
    * 定义ThreadLocal通过泛型，执行存入/取出的数据类型
    *
    * */
    private static ThreadLocal<User> tl = new ThreadLocal<>();
    //将用户存入当前线程(tl)
    public static void setUser(User user){
        tl.set(user);
    }

    //从tl取出当前用户
    public static User getUser(){
        return tl.get();
    }


    //tl数据清空
    public static void remove(){
        tl.remove();
    }

    //获取用户的id
    public static Long getUserId(){
        User user = tl.get();
        return user.getId();
    }
}
